<?php
/**
 * 发票信息
 * User: tengyu
 * Date: 2017/8/28
 * Time: 16:31
 */?>
@extends('common.common')
@section('title','发票-个人中心')
@section('css')
    <link rel="stylesheet" href="{{asset('/css/personal_invoice.css')}}">
@endsection
@section('js')
    <script src="{{asset('/js/personal_invoice.js')}}"></script>
    <script src="{{asset('/js/distpicker.data.js')}}"></script>
    <script src="{{asset('/js/distpicker.js')}}"></script>
    <script src="{{asset('/js/main.js')}}"></script>
    <script>
        /**新增收货地址*/
        function addaddr_submit(id)
        {
            /**数据校验*/
            var rename = $("#ModalBox input[name='rename']").val();
            if(!rename){
                alert('收货人不能为空!');return false;
            }
            var addr_area = $("#province1 option:selected").val();
            if(!addr_area){
                alert('收货地址省/直辖市不能为空!');return false;
            }
            var addr_city = $("#city1 option:selected").val();
            if(!addr_city){
                alert('收货地址市/直辖市郊县不能为空!');return false;
            }
            var addr_town = $("#district1 option:selected").val();
            if(!addr_town && (JSON.stringify(addr_city) != '天津市郊县' || JSON.stringify(addr_city) != '上海市郊县' || JSON.stringify(addr_city) != '重庆市郊县')){
                alert('收货郊县地址不能为空!');return false;
            }
            var addr_detail = $("#ModalBox input[name='addr_detail']").val();
            if(!addr_detail){
                alert('收货详细地址不能为空!');return false;
            }
            /**是否选作默认收货地址判断*/
            var addr_status =$("input[type='checkbox']").is(':checked');
            if(!addr_status){
                addr_status = '0';
            }else{
                addr_status = '1';
            }
            var rephone = $("#ModalBox input[name='rephone']").val();
            if(!rephone){
                alert('收货人联系不能为空!');return false;
            }

            $.ajax({
                url:'/user/addaddr/'+id,
                type:'post',
                headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                data:{
                    'rename':rename,'addr_area':addr_area, 'addr_city':addr_city,
                    'addr_town':addr_town, 'addr_detail':addr_detail,'rephone':rephone,
                    'addr_status':addr_status
                },
                dataType:'json',
                success:function(res){
                    if(res.res == 'ok'){
                        alert(res.msg);
                        history.go(0);
                    }else{
                        alert(res.msg);
                    }
                }
            });
        }
        /**设置默认地址*/
        function default_addr(id)
        {
            $.ajax({
                url:'/user/addr/default/'+id,
                type:'get',
                dataType:'json',
                success:function(res){
                    if(res.res == 'ok'){
                        alert(res.msg);
                        history.go(0);
                    }else{
                        alert(res.msg);
                    }
                }
            });
        }
        /**地址删除*/
        function addr_destroy(id)
        {
            $.ajax({
                url:'/user/addrdestroy/'+id,
                type:'get',
                dataType:'json',
                success:function(res){
                    if(res.res == 'ok'){
                        alert(res.msg);
                        history.go(0);
                    }else{
                        alert(res.msg);
                    }
                }
            });
        }
        /**发票开具*/
        function invoiceinsert()
        {
            var invoice_amount = $("input[name='invoice_amount']").val();
            if(!invoice_amount){
                alert('请填写发票金额!');return false;
            }
            var number = /^[1-9]*[1-9][0-9]*$/;
            if(!number.test(invoice_amount)){
                alert('请输入合法整数金额');return false;
            }
            var invoice_company_tax = $("input[name='invoice_company_tax']").val();
            if(!invoice_company_tax) {
                alert('请填写公司税号!');return false;
            }
            var invoice_company_name = $("input[name='invoice_company_name']").val();
            if(!invoice_company_name){
                alert('请填写公司名!');return false;
            }
            var addr = $("p.nowAdd").text();
            if(!addr){
                alert('请先设置你的收件地址!');return false;
            }
            return true;
        }
        /**确认收到发票*/
        function invoice_ensure(id)
        {
            $.ajax({
                url:'/user/invoice_ensure/'+id,
                type:'get',
                dataType:'json',
                success:function(res){
                    if(res.res == 'ok'){
                        alert(res.msg);
                        history.go(0);
                    }else{
                        alert(res.msg);
                    }
                }
            });
        }
    </script>
@endsection
@section('content')
    <div id="content">
    <!--nav start-->
    @include('common.centernav')
    <!--nav end-->
        <h3>个人中心</h3>
        <div class="container">
            <div class="row">
            <!--sidebar start-->
            @include('usercenter.usersidebar')
            <!--sidebar end-->
                <!--2、右侧主体-->
                <div class="col-xs-9">
                    <!--右侧面版-->
                    <div class="panel panel-default rightPanel">
                        @if (count($errors) > 0)
                        <div class="alert alert-danger text-left">
                            <strong>验证信息有误!</strong>
                            <ul>
                                @if(is_object($errors))
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                @else
                                    <li>{{$errors}}</li>
                                @endif
                            </ul>
                        </div>
                        @endif
                        <div class="panel-heading"><h4>发票信息</h4></div>
                        <div class="panel-body">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#t1" data-toggle="tab">申请发票</a></li>
                                <li><a href="#t2" data-toggle="tab">已申请发票</a></li>
                            </ul>
                            <!--创建内容组-->
                            <div class="tab-content">
                                <!--申请发票内容-->
                                <div class="tab-pane active" id="t1">
                                    <form class="form-horizontal" action="/user/invoiceapply" method="post" onsubmit="return invoiceinsert()">
                                        {!! csrf_field() !!}
                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">开票金额：</label>
                                            <div class="col-xs-5"><input type="text" name="invoice_amount" value="{{old('invoice_amount')}}" class="form-control"placeholder="请输入开票金额"></div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">发票内容：</label>
                                            <div class="col-xs-5">
                                                <select name="invoice_content" value="{{old('invoice_content')}}" id="">
                                                    <option value="1">租赁费</option><option value="2">采购费</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">发票类型：</label>
                                            <div class="col-xs-5 ticket">
                                                <input type="radio" name="invoice_type" value="1" checked>&nbsp;增值税普通发票&nbsp;&nbsp;
                                                <input type="radio" name="invoice_type" value="2">&nbsp;增值税专用发票
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">公司税号：</label>
                                            <div class="col-xs-5"><input type="text" class="form-control" name="invoice_company_tax" value="{{old('invoice_company_tax')}}" placeholder="请输入公司税号"></div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">公司名称：</label>
                                            <div class="col-xs-5"><input type="text" name="invoice_company_name" value="{{old('invoice_company_name')}}" class="form-control"></div>
                                        </div>
                                        <div class="form-group">
                                            @if(count($param['addr'])>0)
                                            <input type="hidden" name="addr_id" value="{{$param['addr'][0]->addr_id }} ">
                                            @else
                                            <input type="hidden" name="addr_id" value="">
                                            @endif
                                            <label class="col-xs-2 control-label">发票收货地址：</label>
                                            <div class="col-xs-10">
                                                <p class="nowAdd">@if(count($param['addr'])>0){{$param['addr'][0]->addr_area}}&nbsp;{{$param['addr'][0]->addr_city}}&nbsp;{{$param['addr'][0]->addr_town}}&nbsp;{{$param['addr'][0]->addr_detail}}@endif</p>
                                                <button id="chooseAddress" class="btn ticketadd">选择地址</button>
                                                <br><br>
                                                <!--选择收货地址-->
                                                <div id="address" class="panel panel-default rightPanel">
                                                    <div class="panel-heading">
                                                        <h4>选择收货地址</h4>
                                                    </div>
                                                    <div class="panel-body">
                                                        <ul class="address">
                                                            @foreach($param['addr'] as $v)
                                                            <li>
                                                            <div class="border <?php if($v->addr_status == '1'){echo 'choosed';};?>">
                                                                <div class="col-xs-1" @if($v->addr_status != '1')onclick="default_addr({{$v->addr_id}})"@endif><b></b></div>
                                                                <div class="col-xs-10">
                                                                    <p>地址:{{$v->addr_area}}&nbsp;{{$v->addr_city}}&nbsp;{{$v->addr_town}}&nbsp;{{$v->addr_detail}}</p>
                                                                    <p>姓名:{{$v->addr_rename}}</p>
                                                                    <p>联系方式:{{$v->addr_rephone}}</p>
                                                                    <p>
                                                                        <span onclick="addr_destroy({{$v->addr_id}})">删除</span>
                                                                        @if($v->addr_status == '1')<span class="rt">已设为默认地址</span>@else<span class="rt" onclick="default_addr({{$v->addr_id}})">设为默认地址</span>@endif
                                                                    </p>
                                                                </div>
                                                            </div>
                                                            </li>
                                                            @endforeach
                                                            <!--添加新地址-->
                                                            <li id="addAddress">
                                                                <div class="border">
                                                                    <div data-toggle="modal" data-target="#ModalBox">+</div>
                                                                    <p>新增收货地址</p>
                                                                </div>
                                                            </li>
                                                        </ul>
                                                    </div>
                                                </div>
                                                <button class="btn btn-warning">提交申请</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <!--已申请发票内容-->
                                <div class="tab-pane" id="t2">
                                    <ul class="ticketHead list-inline">
                                        <li>发票内容</li>
                                        <li>收件信息</li>
                                        <li>开票金额</li>
                                        <li>申请时间</li>
                                        <li>明细状态</li>
                                        <li>操作</li>
                                    </ul>
                                    <?php $invoice_type['1']='增值税普通发票';$invoice_type['2']='增值税专用发票';?>
                                    @if(count($param['invoice_user'])>0)
                                    @foreach($param['invoice_user'] as $k=>$v)
                                    <ul class="ticketBody list-inline">
                                        <li>{{$invoice_type[$v->invoice_type]}}</li>
                                        <li>{{$v->addr_area.' '.$v->addr_city.' '.$v->addr_town.' '.$v->addr_detail}}</li>
                                        <li>{{$v->invoice_amount}}</li>
                                        <li>{{date('Y.m.d',strtotime($v->created_at))}}</li>
                                        <li><button class="btn btn-warning" onclick="alert('物流系统等待对接')">查看物流</button></li>
                                        @if($v->invoice_status==2)
                                        <li><button class="btn btn-warning" onclick="invoice_ensure({{$v->invoice_id}})">确认收货</button></li>
                                        @else
                                        <li>发票已收到</li>
                                        @endif
                                    </ul>
                                    @endforeach
                                    @else
                                    <ul class="ticketBody list-inline text-center">
                                        <img src="{{asset('/img/771509094256_.pic.jpg')}}" alt="暂无数据">
                                    </ul>
                                    @endif
                                </div>
                            </div>
                            <!--开具发票说明-->
                            <p class="ticketInfo">开具发票说明</p>
                            <p class="ticketInfoContent">
                                发票内容可为：租赁票、服务票。<br>发票类型为；增值税普通发票或增值税专用发票。租赁费用开具发票金额不得小于500元，服务费开票金额小于500元需要支付快递费15元。<br><br>特别注意：芈租界仅支持先付款（或充值）后开发票，无法支持在客户付款前为客户开具发票
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--隔断底部-->
    <div id="partition"></div>
    <!--修改地址模态框-->
    <div id="ModalBox" class="modal" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close" data-dismiss="modal">&times;</span><h4 class="modal-title">新增收货地址</h4>
                </div>
                <div class="modal-body">
                    <form class="form-inline">
                        <div class="form-group">
                            <label class="control-label">收货人:</label>
                            <input type="text" class="form-control"  name="rename" value="{{old('rename')}}" placeholder="请输入收货人姓名">
                        </div>
                        <div class="form-group">
                            <label id="district" class="control-label">所在地区:</label>
                            <div id="selectAdd" data-toggle="distpicker">
                                <div class="form-group">
                                    <label class="sr-only" for="province1">Province</label>
                                    <select class="form-control" id="province1"></select>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="city1">City</label>
                                    <select class="form-control" id="city1"></select>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="district1">District</label>
                                    <select class="form-control" id="district1"></select>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label">详细地址:</label>
                            <input type="text" class="form-control" name="addr_detail" value="{{old('addr_detail')}}" placeholder="请输入详细地址">
                        </div>
                        <div class="form-group">
                            <label class="control-label">联系方式:</label>
                            <input type="text" class="form-control" name="rephone" value="{{old('rephone')}}" placeholder="请输入收货人联系方式">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning lf" data-dismiss="modal"  onclick="addaddr_submit({{$user->id}})">提交</button>
                    <input type="checkbox">&nbsp;设置为默认地址
                </div>
            </div>
        </div>
    </div>
@endsection

